package com.ruoyi.meiyijia.service.impl;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.meiyijia.entity.FileMeiyijiaInventory;
import com.ruoyi.meiyijia.mapper.FileMeiyijiaInventoryMapper;
import com.ruoyi.meiyijia.service.InventoryStatisticsService;
import com.ruoyi.meiyijia.vo.FileMeiyijiaInventoryParam;
import com.ruoyi.meiyijia.vo.SaleParam;
import com.ruoyi.meiyijia.vo.SaleTypeVo;
import com.ruoyi.meiyijia.vo.SaleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@Service
/**
 * @author: 王振鹏
 * @date: 2023年05月21日 21:41:00
 * @description:
 */
@Slf4j
@DataSource(value = DataSourceType.MYJ)
public class InventoryStatisticsServiceImpl implements InventoryStatisticsService {
    @Resource
    private FileMeiyijiaInventoryMapper inventoryMapper;

    /**
     * 版本对比
     *
     * @param pre :  前一版本
     * @param aft :  后一版本
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author: 王振鹏
     * @date: 2023/5/21 21:43
     * @throws:
     */
    @Override
    public Map<String, Object> InventoryContrast(SaleParam saleParam) {
        Map<String, Object> result = new HashMap();
       // 对条码做特殊符号转换
        if (org.apache.commons.lang3.StringUtils.isNotBlank(saleParam.getCode())) {
            saleParam.setCode(com.ruoyi.common.utils.StringUtils.escapeLike(saleParam.getCode()));
        }


        List<SaleVo> list = inventoryMapper.selectrDiffVersion(saleParam);

        /**
         * 计算销售利润或者进货利润
         */
        String sum = list.stream().map(item->new BigDecimal(item.getProfitSum()))
                .reduce(BigDecimal.ZERO,BigDecimal::add)
                .setScale(2,BigDecimal.ROUND_HALF_UP).toString();


        /**
         * 销售总额
         */
        String priceSum = list.stream().map(item->new BigDecimal(item.getRetailPriveSum()))
                .reduce(BigDecimal.ZERO,BigDecimal::add)
                .setScale(2,BigDecimal.ROUND_HALF_UP).toString();

        /**
         * 计算销售总件数或者进货总件数
         */
        String counts = list.stream().map(item->new BigDecimal(item.getCount()))
                .reduce(BigDecimal.ZERO,BigDecimal::add)
                .setScale(0,BigDecimal.ROUND_HALF_UP).toString();

        /**
         * 根据分类 计算数量 计算利润
         */
        Map<String,List<SaleVo>> typeMap = list.stream().collect(Collectors.groupingBy(SaleVo::getType));


        List<SaleTypeVo> saleTypeVoList = new ArrayList<>();
        for (String item:
             typeMap.keySet()) {
            SaleTypeVo entity = new SaleTypeVo();
            List<SaleVo> typeList = typeMap.get(item);
            entity.setType(item);
            entity.setList(typeList);
            BigDecimal count = BigDecimal.ZERO;
            BigDecimal money = BigDecimal.ZERO;
            for (SaleVo obj:
            typeList) {
                count = count.add(new BigDecimal(obj.getCount()));
                money = money.add(new BigDecimal(obj.getProfitSum()));
            }
            entity.setMoney(money.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue());
            entity.setCount(count.setScale(0,BigDecimal.ROUND_HALF_UP).intValue());
            saleTypeVoList.add(entity);
        }

        result.put("list",list);
        result.put("sum",sum);
        result.put("priceSum",priceSum);
        result.put("count",counts);
        result.put("typeList",saleTypeVoList);
        return result;
    }
}